<?php

require_once 'classes/database.php';

class User{
	protected $id, $name, $email, $password, $image;
	
	static $dbTableName = "pg_user";
	
	function __construct($idN,$emailN){
		$this->id = $idN;
		$this->email = $emailN;
	}
	
	function setName($new){
		$this->name = $new;
	}
	
	function setImage($new){
		$this->image = $new;
	}
	
	function setPassword($new){
		$this->password = $new;
	}
	
	function getName(){
		return($this->name);
	}
	
	function getPassword(){
		return($this->password);
	}
	
	function getEmail(){
		return($this->email);
	}
	
	function getId(){
		return($this->id);
	}
	
	function getImage(){
		return($this->image);
	}
}

class UserDBUtils{
	private $dbUtils;
	
	function __construct(){
		$this->dbUtils = new DatabaseUtils();
	}
	
	function __destruct(){
		unset($database);
	}
	
	function userFromRow($row){
		if(
			isset($row["email"]) && isset($row["id"]) 
			&& !(trim($row["email"])==='') && !(trim($row["id"])==='') 
		){
			$u = new User($row["id"],$row["name"]);
			$u->setName($row["name"]);
			$u->setImage($row["image"]);
			return($u);
		}else{
			return(NULL);
		}
	}
	
	function checkUserPassword($email,$password){
		$q = "SELECT * FROM ".User::$dbTableName." WHERE email = :email";
		$params =  array(":email"=>$email);
		$result = $this->dbUtils->runSelect($q, $params);
		if(count($result)<1){
			return(array("status"=>FALSE,"message"=>"Email is incorrect"));
		}else{
			foreach ($result as $user){
				if(trim(utf8_encode($user["password"]))===trim(utf8_encode($password))){
					return(array("status"=>TRUE,"message"=>"Password is correct", "user"=>$this->userFromRow($user)));
				}else{
					return(array("status"=>FALSE,"message"=>"Password is incorrect"));
				}
			}
		}
	}
	
	function saveUser($u,$newUser=FALSE){
		$q = "SELECT * FROM ".User::$dbTableName." WHERE email = :email";
		$result = $this->dbUtils->runSelect($q, array(":email"=>$u->getEmail()));
		if(count($result)<1){
			$q = "INSERT INTO ".User::$dbTableName."(name,email,password) VALUES(:name,:email,:password)";
			$params = array(":name"=>$u->getName(),":email"=>$u->getEmail(),":password"=>$u->getPassword());
			$resultSave = $this->dbUtils->runInsert($q, $params);
			
			$q = "SELECT * FROM ".User::$dbTableName." WHERE email = :email";
			$result = $this->dbUtils->runSelect($q, array(":email"=>$u->getEmail()));
		}else{
			if($newUser){
				return(array("status"=>FALSE, "user"=>$u, "message"=>"Email already registered."));
			}else{
				$q = "UPDATE ".User::$dbTableName." SET name=:name, image=:image WHERE email=:email";
				$params = array(":name"=>$u->getName(),":email"=>$u->getEmail(), ":image"=>$u->getImage());
				$resultSave = $this->dbUtils->runUpdate($q, $params);
			}
		}
		foreach ($result as $user){
			$u = $this->userFromRow($user);
		}
		if($resultSave>0){
			return(array("status"=>TRUE, "message"=>"Saved.", "user"=>$u));
		}else{
			return(array("status"=>FALSE, "user"=>$u, "message"=>("Couldn't save user with email: ".$u->getEmail())));
		}
	}
}

$userUtils = new UserDBUtils();
